package com.fzkj.project.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.fzkj.common.core.domain.AjaxResult;
import com.fzkj.project.entiy.*;
import com.fzkj.project.mapper.*;
import com.fzkj.project.service.ISysUserAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户学院权限Service业务层处理
 *
 * @author zhangmx
 * @date 2024-06-27
 */
@Service
public class SysUserAuthServiceImpl implements ISysUserAuthService
{
    @Autowired
    private SysUserAuthMapper sysUserAuthMapper;

    @Autowired
    private ProBjMapper proBjMapper;

    @Autowired
    private ProXyMapper proXyMapper;

    @Autowired
    private ProZyMapper proZyMapper;

    @Autowired
    private ProNjMapper proNjMapper;

    /**
     * 查询用户学院权限
     *
     * @param userId 用户学院权限主键
     * @return 用户学院权限
     */
    @Override
    public SysUserAuth selectSysUserAuthByUserId(Long userId,String type)
    {
        return sysUserAuthMapper.selectSysUserAuthByUserId(userId,type);
    }

    /**
     * 查询用户学院权限列表
     *
     * @param sysUserAuth 用户学院权限
     * @return 用户学院权限
     */
    @Override
    public List<SysUserAuth> selectSysUserAuthList(SysUserAuth sysUserAuth)
    {
        return sysUserAuthMapper.selectSysUserAuthList(sysUserAuth);
    }

    /**
     * 新增用户学院权限
     *
     * @param sysUserAuth 用户学院权限
     * @return 结果
     */
    @Override
    public int insertSysUserAuth(SysUserAuth sysUserAuth)
    {
        return sysUserAuthMapper.insertSysUserAuth(sysUserAuth);
    }

    /**
     * 修改用户学院权限
     *
     * @param sysUserAuth 用户学院权限
     * @return 结果
     */
    @Override
    public int updateSysUserAuth(SysUserAuth sysUserAuth)
    {
        return sysUserAuthMapper.updateSysUserAuth(sysUserAuth);
    }

    /**
     * 批量删除用户学院权限
     *
     * @param userIds 需要删除的用户学院权限主键
     * @return 结果
     */
    @Override
    public int deleteSysUserAuthByUserIds(Long[] userIds)
    {
        return sysUserAuthMapper.deleteSysUserAuthByUserIds(userIds);
    }

    /**
     * 删除用户学院权限信息
     *
     * @param userId 用户学院权限主键
     * @return 结果
     */
    @Override
    public int deleteSysUserAuthByUserId(Long userId)
    {
        return sysUserAuthMapper.deleteSysUserAuthByUserId(userId);
    }

    public AjaxResult listTree(){
        List<Map<String,Object>> respList = new ArrayList<>();
        List<ProXy> proXyList = proXyMapper.selectProXyList(new ProXy());
        List<ProNj> proNjs = proNjMapper.selectProNjList(new ProNj());
        List<ProZy> proZyList = proZyMapper.selectProZyList(new ProZy());
        List<ProBj> proBjs = proBjMapper.selectProBjListOptimized(new ProBj());
        for (ProXy proXy : proXyList) {
            List<ProNj> collectNj = proNjs.stream().filter(l -> proXy.getId().equals(l.getpId())).collect(Collectors.toList());
            List<Map<String,Object>> objectList = new ArrayList<>();

            for (ProNj proNj : collectNj) {
                List<ProZy> collectZy = proZyList.stream().filter(l -> proNj.getId().equals(l.getpId())).collect(Collectors.toList());
                List<Map<String,Object>> objectList2 = new ArrayList<>();
                for (ProZy proZy : collectZy) {
                    List<ProBj> collectBj = proBjs.stream().filter(l -> proZy.getId().equals(l.getpId())).collect(Collectors.toList());
                    //过滤
                    List<Map<String, Object>> collect = collectBj.stream().map(entity -> {
                        Map<String, Object> map = new HashMap<>();
                        map.put("key", entity.getId()+"bj");
                        map.put("id", entity.getId());
                        map.put("name", entity.getName());
                        map.put("type", "bj");
                        return map;
                    }).collect(Collectors.toList());
                    Map<String,Object> objectMap = new HashMap<>();
                    objectMap.put("childList",collect);
                    objectMap.put("name",proZy.getName());
                    objectMap.put("id",proZy.getId());
                    objectMap.put("key", proZy.getId()+"zy");
                    objectMap.put("type","zy");
                    objectList2.add(objectMap);
                }
                Map<String,Object> objectMap = new HashMap<>();
                objectMap.put("childList",objectList2);
                objectMap.put("name",proNj.getName());
                objectMap.put("id",proNj.getId());
                objectMap.put("key", proNj.getId()+"nj");
                objectMap.put("type","nj");
                objectList.add(objectMap);
            }
            Map<String,Object> objectMap = new HashMap<>();
            objectMap.put("childList",objectList);
            objectMap.put("name",proXy.getName());
            objectMap.put("id",proXy.getId());
            objectMap.put("key", proXy.getId()+"xy");
            objectMap.put("type","xy");
            respList.add(objectMap);
        }
        return AjaxResult.success(respList);
    }

    public AjaxResult listUserAuth(Map<String,Object> map){
        Map<String,Object> respMap = new HashMap<>();
        Long userId = Long.valueOf(String.valueOf(map.get("userId")));
        SysUserAuth sysUserAuthPram = new SysUserAuth();
        sysUserAuthPram.setUserId(userId);
        List<SysUserAuth> list = sysUserAuthMapper.selectSysUserAuthList(sysUserAuthPram);
        List<String> collect = list.stream().map(l -> l.getBusId() + l.getType()).collect(Collectors.toList());
        respMap.put("userId",userId);
        respMap.put("authListSelect",collect);
        return AjaxResult.success(respMap);
    }

    @Transactional(rollbackFor = Exception.class)
    public AjaxResult subAuth(Map<String, Object> map){
        List<SysUserAuth> list = new ArrayList<>();
        Long userId = Long.valueOf(String.valueOf(map.get("userId")));
        if (!map.containsKey("authList"))
            return AjaxResult.error("请选择权限");
        List<Map<String,Object>> authList = (List<Map<String,Object>>) map.get("authList");
        for (Map<String, Object> objectMap : authList) {
            String id = String.valueOf(objectMap.get("id"));
            String type = String.valueOf(objectMap.get("type"));
            SysUserAuth sysUserAuth = new SysUserAuth();
            sysUserAuth.setUserId(userId);
            sysUserAuth.setBusId(id);
            sysUserAuth.setType(type);
            list.add(sysUserAuth);
        }
        sysUserAuthMapper.deleteSysUserAuthByUserId(userId);
        //添加权限
        if (CollUtil.isNotEmpty(list)){
            sysUserAuthMapper.batchSysUserAuth(list);
        }
        return AjaxResult.success("操作成功");
    }
}
